package com.banciyuan.bcywebview.biz.post.auto;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2016/1/6.
 */
public class AutoComplateNoLimteAdapter extends BaseAdapter implements Filterable {
    private List<String> slist = new ArrayList<String>();

    private Context mContext;

    private LayoutInflater inflater;

    private ArrayFilter arrayFilter;

    private String orign;


    public AutoComplateNoLimteAdapter(List<String> slist, Context mContext, String orign) {
        this.slist = slist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.orign = orign;
        arrayFilter = new ArrayFilter();
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int i) {
        return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Filter getFilter() {
        if (arrayFilter == null)
            return new ArrayFilter();
        else
            return arrayFilter;
    }

    private class ViewHodler {
        TextView title;
        TextView front;
        TextView back;
        LinearLayout background;

        public ViewHodler(View view) {
            title = (TextView) view.findViewById(R.id.auto_complate);
            front = (TextView) view.findViewById(R.id.auto_front);
            back = (TextView) view.findViewById(R.id.auto_back);
            background = (LinearLayout) view.findViewById(R.id.auto_background);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHodler viewHodler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.autocomplate_item, null);
            viewHodler = new ViewHodler(view);
            view.setTag(viewHodler);
        } else {
            viewHodler = (ViewHodler) view.getTag();
        }

        viewHodler.front.setVisibility(View.GONE);
        viewHodler.back.setVisibility(View.GONE);
        viewHodler.title.setText(slist.get(i));
        viewHodler.title.setTextColor(mContext.getResources().getColorStateList(R.color.mine_textcolor));

        return view;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            FilterResults results = new FilterResults();

            results.values = slist;
            results.count = slist.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            slist = (List<String>) filterResults.values;
            notifyDataSetChanged();
        }
    }
}
